package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.workflow.bean.Outputcard;
import com.workflow.bean.Outputdeptdetail;
import com.workflow.service.OutputdeptdetailService;

public class OutputdeptdetailServiceImpl extends AbstractManager implements
		OutputdeptdetailService {

	public void addOutputdeptdetail(Outputdeptdetail outputdeptdetail) {
		String hql = "select count(*) from Outputdeptdetail op where op.outputdept.outputno = ?";
		long count = 0;
		count = (Long)this.getHibernateTemplate().find(hql,outputdeptdetail.getOutputdept().getOutputno()).get(0);
		count %= 1000L;
		
//		Long num = Long.parseLong(DateHelper.dateToStr(new Date(), "yyyyMMdd"));
//		num *= 1000L;		
//		num += count;
		
		String outputno = outputdeptdetail.getOutputdept().getOutputno();
		
		String outdetailno = "CKD-" + outputno.substring(outputno.length()-9, outputno.length()) + count;
		outputdeptdetail.setOutdetailno(outdetailno);
		
		this.getHibernateTemplate().save(outputdeptdetail);
	}

	@SuppressWarnings("unchecked")
	public List<Outputdeptdetail> findAllOutputdeptdetail() {
		String hql = "from Outputdeptdetail od order by od.outputdept.outputno ,od.outdetailno";
		return this.getHibernateTemplate().find(hql);
	}

	public Outputdeptdetail findOutputdeptdetail(String outdetailno) {
		return (Outputdeptdetail) this.getHibernateTemplate().load(Outputdeptdetail.class, outdetailno);
	}

	public PagerModel findOutputdeptdetailForPager() {
		String hql = "from Outputdeptdetail od order by od.outputdept.outputno ,od.outdetailno";
		return searchPaginated(hql);
	}

	public PagerModel findOutputdeptdetailForPager(String outputno) {
		String hql = "from Outputdeptdetail od where od.outputdept.outputno = ? order by od.outdetailno";
		return searchPaginated(hql,outputno);
	}

	@SuppressWarnings("unchecked")
	public void removeOutputdeptdetail(String outdetailno) {
		Outputdeptdetail outputdeptdetail = null;
		outputdeptdetail = (Outputdeptdetail) this.getHibernateTemplate().load(Outputdeptdetail.class, outdetailno);
		
		if(outputdeptdetail != null) {
			String hql = "from Outputcard card where card.outdetailno = ? order by card.cardno";
			List<Outputcard> outputcardlist = this.getHibernateTemplate().find(hql,outdetailno);
			for(Outputcard outputcard : outputcardlist) {
				Cardinfo cardinfo = null;
				cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, outputcard.getCardno());
				Datadict datadictByStatus = new Datadict();
//				置卡状态为库存 状态编号 76l
				datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
				cardinfo.setDatadictByStatus(datadictByStatus);
				cardinfo.setBelongto("");//取消所属状态，临时
				this.getHibernateTemplate().update(cardinfo);
				this.getHibernateTemplate().delete(outputcard);
			}
			this.getHibernateTemplate().delete(outputdeptdetail);
		}
	}

	public void updateOutputdeptdetail(Outputdeptdetail outputdeptdetail) {
		this.getHibernateTemplate().update(outputdeptdetail);
	}

}
